package com.caregiver.watch.business.user.controller;

import com.caregiver.watch.business.user.model.dto.LoginByCaptchaParam;
import com.caregiver.watch.business.user.model.dto.LoginParam;
import com.caregiver.watch.business.user.model.vo.AccessTokenVo;
import com.caregiver.watch.business.user.service.LoginService;
import com.caregiver.watch.common.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 登录控制器
 * @Author 疆戟
 * @Date 2025/8/17 11:54
 * @Version 1.0
 */
@Tag(name  = "LoginController", description = "登录")
@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Operation(summary = "账号密码登录", description = "账号密码登录")
    @PostMapping("login")
    public R<AccessTokenVo> login(@RequestBody LoginParam loginParam) {
        return R.ok(loginService.userLogin(loginParam.getUsername(), loginParam.getPassword()));
    }

    @Operation(summary = "验证码登录", description = "通过手机号和验证码登录")
    @PostMapping("loginByCaptcha")
    public R<AccessTokenVo> loginByCaptcha(@RequestBody LoginByCaptchaParam loginParam) {
        return R.ok(loginService.userLoginPhone(loginParam.getPhone(), loginParam.getCode()));
    }


    @Operation(summary = "退出登录", description = "退出登录")
    @DeleteMapping("logout")
    public R<Boolean> logout(HttpServletRequest request) {
        return R.ok(loginService.logout(request));
    }
}
